package com.nit.lab.vote.controller.basic;

import com.alibaba.fastjson.JSONObject;
import com.nit.lab.vote.dto.system.TokenDTO;
import com.nit.lab.vote.dto.utils.OpResultDTO;
import com.nit.lab.vote.dto.basic.UserDTO;
import com.nit.lab.vote.dto.utils.TableReqDTO;
import com.nit.lab.vote.dto.utils.TableRspDTO;
import com.nit.lab.vote.service.basic.UserService;
import com.nit.lab.vote.utils.HttpRequestReader;
import com.nit.lab.vote.utils.JWTUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping(value = "/api/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * @Description: 登录
     * @Date: 2020/12/10
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public OpResultDTO login() {
        OpResultDTO op = new OpResultDTO();
        return op;
    }

    /**
     * @Description: 批量导入用户信息
     * @Date: 2020/12/10
     */
    @RequestMapping(value = "/addList", method = RequestMethod.POST)
    public OpResultDTO addList(@RequestBody List<UserDTO> userDTOS) {
        OpResultDTO op = new OpResultDTO();
        try {
            op = userService.addList(userDTOS);
        } catch (Exception e) {
            op.setMessage("error");
            op.setResult("导入失败");
            System.out.println(e.toString());
        }
        return op;
    }

    /**
     * @Description: 批量获取用户信息
     * @Date: 2020/12/10
     */
    @RequestMapping(value = "/listAll", method = RequestMethod.POST)
    public OpResultDTO listAll(HttpServletRequest request) {
        OpResultDTO op = new OpResultDTO();
        try {
            op = userService.listAll();
        } catch (Exception e) {
            op.setMessage("error");
            op.setResult("查询失败");
            System.out.println(e.toString());
        }
        return op;
    }



    /**
     * @Description: 批量获取评委信息
     * @Date: 2020/12/10
     */
    @RequestMapping(value = "/listAllJudge", method = RequestMethod.POST)
    public TableRspDTO listAllJudge(@RequestHeader(name = "Access-Token") String token, @RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = null;
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            if (tokenDTO != null && tokenDTO.getRoleId() == 1) {
                tableRspDTO = userService.listAllJudge(tableReqDTO);
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return tableRspDTO;
    }

    /**
     * @Description: 管理员添加评委
     * @Date: 2020/12/15
     */
    @RequestMapping(value = "/addJudge", method = RequestMethod.POST)
    public OpResultDTO addJudge(@RequestHeader(name = "Access-Token") String token, @RequestBody UserDTO userDTO) {
        OpResultDTO op = new OpResultDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            if (tokenDTO == null) {
                op.setMessage("error");
                op.setResult("用户信息有误");
            } else {
                if (tokenDTO.getRoleId() > 1) {
                    op.setMessage("error");
                    op.setResult("用户权限有误");
                } else {
                    op = userService.addJudge(userDTO);
                }
            }
        } catch (Exception e) {
            op.setMessage("error");
            op.setResult("添加失败");
            System.out.println(e.toString());
        }
        return op;
    }

    /**
     * @Description: 管理员修改评委
     * @Date: 2020/12/15
     */
    @RequestMapping(value = "/updateJudge", method = RequestMethod.POST)
    public OpResultDTO updateJudge(@RequestHeader(name = "Access-Token") String token, @RequestBody UserDTO userDTO) {
        OpResultDTO op = new OpResultDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            if (tokenDTO == null) {
                op.setMessage("error");
                op.setResult("用户信息有误");
            } else {
                if (tokenDTO.getRoleId() > 1) {
                    op.setMessage("error");
                    op.setResult("用户权限有误");
                } else {
                    op = userService.updateJudge(userDTO);
                }
            }
        } catch (Exception e) {
            op.setMessage("error");
            op.setResult("更新失败");
            System.out.println(e.toString());
        }
        return op;
    }

    /**
     * @Description: 管理员删除评委
     * @Date: 2020/12/15
     */
    @RequestMapping(value = "/removeJudge", method = RequestMethod.POST)
    public OpResultDTO removeJudge(@RequestHeader(name = "Access-Token") String token,
                                   HttpServletRequest request) {
        OpResultDTO op = new OpResultDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            if (tokenDTO == null) {
                op.setMessage("error");
                op.setResult("用户信息有误");
            } else {
                if (tokenDTO.getRoleId() > 1) {
                    op.setMessage("error");
                    op.setResult("用户权限有误");
                } else {
                    JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
                    String userId = jsonRequest.getString("userId");
                    op = userService.removeJudge(userId);
                }
            }
        } catch (Exception e) {
            op.setMessage("error");
            op.setResult("删除失败");
            System.out.println(e.toString());
        }
        return op;
    }

    /**
     * @Description: 登录手机号重复验证
     * @Date: 2020/12/15
     */
    @RequestMapping(value = "/validatePhone", method = RequestMethod.POST)
    public OpResultDTO validatePhone(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            String phone = jsonRequest.getString("phone");
            if (userService.phoneCheck(phone) == null) {
                opResult.setMessage("success");
            } else {
                opResult.setMessage("error");
            }
        } catch (Exception e) {
            System.out.println(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }
}
